package v1

import (
	"errors"
	"fmt"
	"gin_cli/pkg/e"
	"gin_cli/pkg/wechat"
	"gin_cli/service"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Wechat struct {
}

type tpl struct {
	Openid  string                 `json:"openid" binding:"required"`
	TplId   string                 `json:"tpl_id" binding:"required"`
	TplData map[string]interface{} `json:"tpl_data" binding:"required"`
}

func (this *tpl) Validate() error {
	fmt.Println("Validate", this.Openid == "")
	if this.Openid == "" {
		return errors.New("openid 不能为空")
	}
	if this.TplId == "" {
		return errors.New("tpl_id 不能为空")
	}

	return nil
}

// func (this *tpl) Valid(v *validation.Validation) {

// }

func (this *Wechat) SendTpl(c *gin.Context) {
	body := tpl{}
	err := c.ShouldBindJSON(&body)
	if err != nil {
		panic(err)
	}

	wechatSDK := wechat.WechatSDK
	accessToken := service.GetWechatAccessToken()
	result, err := wechatSDK.SendTpl(
		accessToken,
		body.Openid,
		body.TplId,
		body.TplData,
	)
	if err != nil {
		panic(err)
	}
	c.JSON(
		http.StatusOK,
		e.GetResponse(e.SUCCESS, result),
	)
}

type share struct {
	Iv            string `"json:iv"`
	EncryptedData string `"json:encryptedData`
}

func (this *Wechat) GetShareInfo(c *gin.Context) {
	// buf := make([]byte, 1024)
	// n, _ := c.Request.Body.Read(buf)
	// fmt.Println(string(buf[0:n]))
	buf, _ := c.GetRawData()
	var str string = string(buf[0:len(buf)])
	fmt.Println(str)

	c.JSON(
		http.StatusOK,
		e.GetResponse(e.SUCCESS, "222222"),
	)
}
